#include "MainTopTouchBar.h"
#include "LoadTextInfo.h"
#include "SetTextInfo.h"


MainTopTouchBar::MainTopTouchBar()
{
}

MainTopTouchBar::~MainTopTouchBar()
{
}

bool MainTopTouchBar::init()
{
	bool bRet = false;
	do
	{
		setContentSize(CCSizeMake(640, 240));
		setTouchEnabled(true);
		InitPlayerInfo();
		registerWithTouchDispatchar();
		PlayerInfo->setVisible(false);
		bRet = true;

	} while (0);

	return bRet;
}

void MainTopTouchBar::registerWithTouchDispatchar()
{
	auto touchListener = EventListenerTouchOneByOne::create();
	touchListener->onTouchBegan = CC_CALLBACK_2(MainTopTouchBar::ccTouchbegan, this);
	//touchListener->onTouchMoved = CC_CALLBACK_2(MainTopTouchBar::onTouchMoved, this);
	touchListener->onTouchEnded = CC_CALLBACK_2(MainTopTouchBar::ccTouchended, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
}


bool MainTopTouchBar::ccTouchbegan(CCTouch* pTouch, CCEvent* pEvent)
{
	CCPoint pt = pTouch->getLocationInView();
	if (pt.y <= 130 && pt.y >= 0)
	{
		PlayerInfo->setVisible(true);
		return true;
	}
	else return false;
}

void MainTopTouchBar::ccTouchended(CCTouch* pTouch, CCEvent* pEvent)
{
	PlayerInfo->setVisible(false);
}

void MainTopTouchBar::InitPlayerInfo()
{
	PlayerInfo = CCNode::create();
	CCSprite* tipSprite = CCSprite::createWithSpriteFrameName("tipsbase.png");
	tipSprite->setScale(1.5f);
	tipSprite->setPosition(ccp(320, 680));
	PlayerInfo->addChild(tipSprite);
	std::string strtext;
	
	strtext = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("sysinfo_rank");
	CCLabelTTF* player_rank = CCLabelTTF::create(strtext.c_str(), "Arial", 20);
	player_rank->setPosition(ccp(320, 750));
	PlayerInfo->addChild(player_rank);

	strtext = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("sysinfo_level");
	CCLabelTTF* player_level = CCLabelTTF::create(strtext.c_str(), "Arial", 20);
	player_level->setPosition(ccp(320, 722));
	PlayerInfo->addChild(player_level);

	strtext = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("sysinfo_exp");
	CCLabelTTF* player_exp = CCLabelTTF::create(strtext.c_str(), "Arial", 20);
	player_exp->setPosition(ccp(320, 694));
	PlayerInfo->addChild(player_exp);

	strtext = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("sysinfo_charm");
	CCLabelTTF* player_charm = CCLabelTTF::create(strtext.c_str(), "Arial", 20);
	player_charm->setPosition(ccp(320, 666));
	PlayerInfo->addChild(player_charm);

	strtext = TextLoadMgr::SharedTextLoadMgr()->GetStringForKey("sysinfo_needexp");
	CCLabelTTF* player_needexp = CCLabelTTF::create(strtext.c_str(), "Arial", 20);
	player_needexp->setPosition(ccp(320, 638));
	PlayerInfo->addChild(player_needexp);

	CCLabelTTF* player_time = CCLabelTTF::create(GetTime().c_str(), "Arial", 20);
	player_time->setTag(555);
	player_time->setPosition(ccp(320, 610));
	PlayerInfo->addChild(player_time);


	addChild(PlayerInfo);
	schedule(schedule_selector(MainTopTouchBar::updateTime, 1.0f));
}

std::string MainTopTouchBar::GetTime()
{
	struct tm* _tm;
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	{
		time_t temp;
		time(&temp);
		_tm = localtime(&temp);
	}
	#else
	{
		struct cc_timeval now;
		gettimeofday(&now,NULL);
		_tm =  localtime(&now.tv_sec);
	}

	#endif


	int year = _tm->tm_year + 1900;
	int month = _tm->tm_mon + 1;
	int day = _tm->tm_mday;
	int hour = _tm->tm_hour;
	int min = _tm->tm_min;
	int sec = _tm->tm_sec;

	char pTime[20];

	sprintf(pTime, "%02d:%02d:%02d", hour, min, sec);
	std::string strtime(pTime);

	return strtime;
}


void MainTopTouchBar::updateTime(float diff)
{
	(dynamic_cast<CCLabelTTF*>(PlayerInfo->getChildByTag(555))->setString(GetTime().c_str()));
}